<template>
  <div class="promission">
    <div style="margin-bottom: 20px">
      <a-button type="primary" @click="showPort">新增端口</a-button>
    </div>
    <a-table
      :columns="columns" 
      :data-source="portList"
    >
      <span slot="action" slot-scope="record">
        <a-popconfirm
          title="确认删除?"
          ok-text="确定"
          cancel-text="取消"
          @confirm="remove(record)"
        >
          <a>删除</a>
        </a-popconfirm>
      </span>
    </a-table>
    <!-- 新增框 -->
    <a-modal
      title="新增端口"
      :visible="portVis"
      :confirm-loading="portLoad"
      @ok="addPort"
      @cancel="portVis = false"
    >
      <a-form-model :model="form" :label-col="{ span: 6 }" :wrapper-col="{ span: 12 }">
        <a-form-model-item label="protobuff端口">
          <a-input v-model="form.proto" size="small"></a-input>
        </a-form-model-item>
        <a-form-model-item label="rpc端口">
          <a-input v-model="form.rpc" size="small"></a-input>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import moment from 'moment'
import {
    getMillPortConfigList,
    addMillPortConfig,
    delMillPortConfig
  } from '@/services/port.js'
export default {
  name: 'Port',
  data() {
    return {
      portVis: false,
      portLoad: false,
      form: {
        proto: '',
        rpc: ''
      },
      columns: [
        {
          title: 'id',
          dataIndex: 'id',
          key: 'id',
        }, {
          title: 'Proto端口',
          dataIndex: 'protoPort',
          key: 'protoPort',
        }, {
          title: 'rpc端口',
          dataIndex: 'rpcPort',
          key: 'rpcPort',
        }, {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTime',
        }, {
          title: '操作',
          key: 'action',
          scopedSlots: { customRender: 'action' },
        }
      ],
      portList: []
    }
  },
  computed: {
    ...mapGetters('account', ['user'])
  },
  mounted() {
    this.getData()
  },
  methods: {
    /**
     * @desc 获取端口列表
     */
    getData() {
      const formData = new FormData()
      formData.append('userId', this.user.userId)
      getMillPortConfigList(formData).then(res => {
        if(res.data.code == 200) {
          res.data.result.forEach(item => {
            item.createTime = moment(item.createTime).format('YYYY-MM-DD HH:mm')
          })
          this.portList = res.data.result
        }
      })
    },
    /**
     * @desc 新增框
     */
    showPort() {
      this.portVis = true
    },
    /**
     * @desc 确认
     */
    addPort() {
      this.portLoad = true
      const formData = new FormData()
      formData.append('userId', this.user.userId)
      formData.append('protoPort', this.form.proto)
      formData.append('rpcPort', this.form.rpc)
      addMillPortConfig(formData).then(res => {
        if(res.data.code == 200) {
          this.$message.success(res.data.result)
          this.portVis = false
          this.portLoad = false
          this.getData()
        } else {
          this.portLoad = false
          this.$message.warning(res.data.result)
        }
      })
    },
    /**
     * @desc 删除
     */
    remove(record) {
      const formData = new FormData()
      formData.append('userId', this.user.userId)
      formData.append('id', record.id)
      delMillPortConfig(formData).then(res => {
        if(res.data.code == 200) {
          this.$message.success(res.data.result)
          this.getData()
        } else {
          this.$message.warning(res.data.result)
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
  .promission {
    background-color: @base-bg-color;
    padding: 24px;
  }
</style>